//
//  Person.m
//  XCTestDemo
//
//  Created by zero on 2017/6/5.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "Person.h"

@implementation Person

+ (instancetype)personWithDictionary:(NSDictionary*)dict{
    Person* obj = [[self alloc]init];
    [obj setValuesForKeysWithDictionary:dict];
    if(obj.age <= 0 || obj.age > 130){
        obj.age = 1;
    }
    return obj;
}

/** 异步加载个人记录 */
+ (void)loadPersonAsync:(void (^)(Person *))completion {
    
    // 异步 子线程执行
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        // 模拟网络延迟 2s
        [NSThread sleepForTimeInterval:2.0];
        
        Person *person = [Person personWithDictionary:@{@"name":@"abc", @"age":@25}];
        
        // 回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion != nil) {
                completion(person);
            }
        });
    });
}

- (NSString*)description{
    return [NSString stringWithFormat:@"{\nname:%@\nage:%li\n}",self.name,self.age];
}
@end
